﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using TV4Home.Server.TVEInteractionLibrary;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;
using TV4Home.WebInterface.UI.Code;

namespace TV4Home.WebInterface.UI
{
    public partial class Settings : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            SaveSettings();

            (Master as WebSiteMaster).ReloadUpdatePanel();
        }

        private void SaveSettings()
        {
            WebsiteSettings.ShowChannelNameIfChannelPictureIsPresent = this.chkShowChannelName.Checked;

            int hours;
            if (Int32.TryParse(txtTVGuideHoursToShow.Text, out hours))
            {
                if (hours <= 0 || hours > 48)
                    return;

                WebsiteSettings.TVGuideHoursToShow = hours;
            }
        }

        public override void UpdateUI()
        {
            UpdateSettings();
            UpdateTVCards();
            UpdateStreamClients();
        }

        private void UpdateSettings()
        {
            try
            {
                this.chkShowChannelName.Checked = WebsiteSettings.ShowChannelNameIfChannelPictureIsPresent;
                this.txtTVGuideHoursToShow.Text = WebsiteSettings.TVGuideHoursToShow.ToString();
            }
            catch (TypeInitializationException ex) // if the database can't be reached, this exception will be thrown
            {
                if (ex.InnerException != null)
                    throw ex.InnerException;
            }
        }

        private void UpdateTVCards()
        {
            //List<WebCard> cards = TVEInteraction.Instance.GetCards();

            //foreach (WebCard card in cards)
            //{
            //    TableRow row = new TableRow();

            //    row.Cells.Add(new TableCell() { Text = card.Name });
            //    row.Cells.Add(new TableCell() { Text = card.DevicePath });

            //    this.tvCards.Rows.Add(row);
            //}

            //// no cards
            //if (this.tvCards.Rows.Count == 1)
            //{
            //    TableRow tr = new TableRow();
            //    tr.Cells.Add(new TableCell() { Text = "No TV cards" });
            //    this.tvCards.Rows.Add(tr);
            //}
        }

        private void UpdateStreamClients()
        {
            //List<WebRtspClient> clients = TVEInteraction.Instance.GetStreamingClients();

            //foreach (WebRtspClient cl in clients)
            //{
            //    TableRow row = new TableRow();

            //    row.Cells.Add(new TableCell() { Text = Dns.GetHostEntry(cl.IpAdress).HostName });
            //    row.Cells.Add(new TableCell() { Text = cl.Description });
            //    row.Cells.Add(new TableCell() { Text = cl.IsActive.ToString() });
            //    row.Cells.Add(new TableCell() { Text = cl.DateTimeStarted.ToLocalTime().ToString() });

            //    this.clientStreams.Rows.Add(row);
            //}

            //// no clients
            //if (this.clientStreams.Rows.Count == 1)
            //{
            //    TableRow tr = new TableRow();
            //    tr.Cells.Add(new TableCell() { Text = "No streaming clients" });
            //    this.clientStreams.Rows.Add(tr);
            //}
        }
    }
}
